/*
作者：江怀玉
日期：2014-05-10
功能：数据查询功能
解决：快速搜索数据问题

-------------------------------------
依赖:
1,jQuery,
2,jQuery.Data
3,相关列表界面
4,样式文件
5,

const_QueryField="RoleName"，快搜
const_DataFields="[{key:'RoleID',label:'RoleID',width:10,sortable:true,resizeable:true},{key:'iSun_Selected',label:'',width:10,formatter:"checkbox"},{key:'RoleName',label:'角色名称',width:100,sortable:true,resizeable:true},{key:'Remark',label:'描述',width:500,sortable:true,resizeable:true}]
---------------------------------------------------------*/
(function ($) {
    /*
    f:字段对象或快搜字段
    t:搜索类型
    q:查询方法
    callback:回调方法
    */
    $.fn.DataQuery = function (f, fs/*,t*/, callback, q) {
        var defaults = {
            field: f,                       //快搜字段
            datafields: fs,                 //字段列表
            //type:type,                   //查询类型
            operation: [{ Text: "包含", Value: "like" }, { Text: "等于", Value: " = " }, { Text: "大于", Value: ">" }, { Text: "大于且等于", Value: ">=" }, { Text: "小于", Value: "<" }, { Text: "小于且等于", Value: "<=" }, { Text: "不等于", Value: "<>" }, { Text: "不包含", Value: "not like" }, { Text: "在.#.之间", Value: "between"}],
            orand: [{ Text: " ", Value: " " }, { Text: "与", Value: "and" }, { Text: "或", Value: "or"}]
        };
        $(this).click(HightDialogBulider);
        //高级查询界面
        function HightDialogBulider() {
            //debugger
            var data = fnFormatQueryTable();
            var X = $(this).offset().top;
            var Y = $(this).offset().left;
            //$("#div_gridshow").html("");
            if ($("#div_list_query").attr("id") == undefined) {
                $('body').append('<div id="div_list_query" style="z-index: 1000;border: 1px solid #A8A8A8;border-top: 1px solid #A8A8A8;width:400px;height:320px;position: absolute; background-color: #fff; display: none;">' +
                '<div style="height:1px;width:450px; background-color: #6699cc;"><span class=\"label label-important close\" style=\"margin-top:3px;\">X</span></div>' +
                '<div style="height:27px;width:450px;border-bottom: 1px solid #ccc; background-color: #F5FFAA;padding-top:3px;padding-left:5px;"><label>快捷搜索</label>' +
                //'<label>快捷搜索:</label>与或<input type="checkbox" id="checkAndOr1"><input value="" id="QueryContext1" style="width:240px"><input type="button" value="搜" id="btn_list_Quick_Query">' +
                '</div>' +
                '<div style="height:280px;widht:450px;" id="div_list_query_data"></div>' +
                '<div style="height:45px; margin-bottom:0px;width:450px; background-color:  #6699cc;text-align:right;padding:5px;">' +
                '<button type="button"  id="btn_Query_OK" value="" class="btn btn-info">查询</button>' +
                '<button type="button"  style="margin-right:0px;" id="btn_Query_Empty" value="" class="btn btn-info">清空</button>' +
                '<button type="button"  style="margin-right:0px;" id="btn_Query_Cancel" value="" class="btn btn-info">关闭</button>' +
                '<button type="button"  style="margin-right:0px;" id="btn_Query_Hight" value="" class="btn btn-info">高级查询</button>' +
                '</div>');
                $("#div_list_query").height(350);
                $("#div_list_query").width(450);
                var sbhtml = '<table class="querytable" style="widht:400px"><thead><th style="padding-left:5px;">列名</th><th style="padding-left:5px;">操作</th><th style="padding-left:5px;">值</th></thead>';
                if (data != "") {
                    sbhtml += '<tbody>' + data + '</tbody>';
                } else {
                    sbhtml += '<tbody><tr><td style="color:red;text-align:center;width:400px;" colspan=3>没有定义列</td></tr></tbody>';
                }
                $("#div_list_query_data").html(sbhtml);
                $("#btn_Query_OK").click(function () {
                    var condition = getCondtionStr();
                    if (condition != "") {
                        //debugger
                        fnQueryRe(encodeURI(condition));
                        $("#div_list_query").hide();
                    }
                    else {
                        alert("请输入值进行搜索");
                    }
                });
                $("#btn_Query_Cancel").click(function () {
                    $("#div_list_query").hide();
                });
                $("#div_list_query .close").click(function () {
                    $("#div_list_query").hide();
                });
                $("#btn_Query_Empty").click(function () {
                    $("#div_list_query_data input").val("");
                });
                $("#btn_Query_Hight").click(function () {
                    $("#div_list_query").hide();
                    fnHeightQuery();
                });

                $("#btn_list_Quick_Query").click(function () {
                    var condition = fnQuciklyQuery();
                    if (condition != "") {
                        fnQueryRe(condition);
                        // alert(condition);
                        $("#div_list_query").hide();
                    }
                    else {
                        alert("请输入值进行搜索");
                    }
                });
            }
            $("#div_list_query").css("left", Y + 100).css("top", X + 25).show();
        }


        function getCondtionStr() {
            var $tr = $("#div_list_query table tbody tr");
            var _c = ""; //条件
            $tr.each(function (e) {
                var _v = $(this).find("input")[0].value;
                if (_v != "") {
                    var _f = $(this).attr("id").replace("tr_", "");
                    var _o = $($(this).find("select")[0]).val();
                    var arrayc = new Array();              //值
                    arrayc = _v.split('#');
                    _v = arrayc.length > 1 ? ("'" + arrayc[0] + "' and '" + arrayc[1] + "'") : (_o.indexOf("like") >= 0 ? ("'%" + arrayc[0] + "%'") : ("'" +arrayc[0] + "'"));
                    _s = _f + " " + _o + " " + _v;
                    _c = _c == "" ? _s : _c + " and " + _s;
                }
            });
            //alert(_c);
            return _c;
           
        }

        ///-------------------------------------------
        ///快速搜索
        ///-------------------------------------------
        var fnQuciklyQuery = function () {

            if (defaults.field == undefined || defaults.field == "") {
                return;
            }
            var _keyword = document.getElementById("QueryContext1").value;
            if (_keyword == "") {
                return;
            }
            var _chk = document.getElementById("checkAndOr1").checked;
            //alert(_chk);
            var _opr = _chk ? " and " : " or ";
            //alert(_opr);
            var _fields = defaults.field.split(",");
            var _keys = _keyword.split(" ");
            var _condition = "";
            for (n = 0; n < _keys.length; n++) {
                if (_keys[n] == "") {
                    continue;
                }
                if (n == 0) {
                    _condition = " ( "
                }
                else {
                    _condition = _condition + _opr + " ( ";
                }
                for (i = 0; i < _fields.length; i++) {
                    if (i == 0) {
                        _condition = _condition + " (" + _fields[i] + " like '%" + _keys[n] + "%')  ";
                    }
                    else {
                        _condition = _condition + " or (" + _fields[i] + " like '%" + _keys[n] + "%')  "
                    }
                }
                _condition = _condition + " )";
            }
            //alert(_condition);
            return ((_condition));
        }

        var fnFormatQueryTable = function () {
            var sehtml = fnFormatSelectOption();
            var sbhtml = "";
            for (i = 2; i < defaults.datafields.length; i++) {
                sbhtml = sbhtml + '<tr id="tr_' + defaults.datafields[i].key + '"><td style="width:115px;padding-left:5px;">' + defaults.datafields[i].label + '</td><td>' + sehtml + '</td><td><input style="width:200px"><td></tr>';
            }
            return sbhtml;
        }

        var fnFormatSelectOption = function () {
            var str1 = [];
            str1.push("<select style='width:80px'>");
            for (i = 0; i < defaults.operation.length; i++) {

                str1.push("<option value='" + defaults.operation[i].Value + "'>" + defaults.operation[i].Text + "</option>");
            }
            str1.push("</select>");
            return str1.join("");
        }

        //快搜生成&一般查询
        function QuickDialogBulider() {

        }
    }
})(jQuery);

//初始化控件
$(document).ready(function () {
    //$("#btnQuery").attr("onclick", "");
    $("#btnQuery").DataQuery(const_QueryField, const_DataFields);
    setTimeout(function(){
    $("input[type=button]").addClass("btn");
    $("input[type=button]").addClass("btn-info");
    },100);
    //$("#btnQuery").click();
});

